//TextField  文本输入框 类似于EditText

//构造函数 :
//const TextField({
//Key key,
//this.controller,//控制器，控制TextField文字
//this.focusNode,
//this.decoration: const InputDecoration(),//输入器装饰
//TextInputType keyboardType: TextInputType.text,//输入的类型
//this.style,
//this.textAlign: TextAlign.start,//对齐方式
//this.autofocus: false,
//this.obscureText: false,//是否隐藏输入
//this.autocorrect: true,
//this.maxLines: 1,
//this.maxLength,
//this.maxLengthEnforced: true,
//this.onChanged,//文字改变触发
//this.onSubmitted,//文字提交触发（键盘按键）
//this.inputFormatters,
//})

import 'package:flutter/material.dart';
import 'package:flutter_practice/utils/Utils.dart';

class MyTextFieldDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new MyTextFieldState();
  }
}

class MyTextFieldState extends State<MyTextFieldDemo> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar: Utils.getAppBar('文本输入框'),
      body: TextField(
        keyboardType: TextInputType.number,
        decoration: InputDecoration(
          contentPadding: EdgeInsets.only(top: 10.0), //内容边距
          icon: Icon(Icons.phone), // 提示图片
          helperText: '注册需要填写手机号码', //提示文字
          labelText: '请输入你的号码', //下面辅助文字
        ),
        autofocus: false,
        onChanged: (String str) {
          print('用户变更: $str');
        },
        onSubmitted: (String str) {
          print('用户提交$str');
        },
      ),
    );
  }
}

//练习

class LoginDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new LoginState();
  }
}

class LoginState extends State<LoginDemo> {
  var _userNameCon = new TextEditingController();
  var _passCon = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return new Scaffold(
      appBar: Utils.getAppBar('输入框练习'),
      body: Column(
        children: <Widget>[
          TextField(
            decoration: InputDecoration(
              icon: Icon(Icons.person),
              labelText: '请输入用户名',
              helperText: '我们需要你注册一个用户名',
            ),
            autofocus: false,
            controller: _userNameCon,
          ),
          TextField(
            decoration: InputDecoration(
              icon: Icon(Icons.lock),
              labelText: '请输入文本',
              helperText: '请输入数字',
            ),
            autofocus: false,
            keyboardType: TextInputType.number,
            obscureText: true,
            controller: _passCon,
          ),
          Builder(builder: (BuildContext context) {
            return new RaisedButton(
              onPressed: () {
                if (_userNameCon.text.toString() == "nihao" &&
                    _passCon.text.toString() == '123') {
                  Scaffold
                      .of(context)
                      .showSnackBar(SnackBar(content: Text('登陆成功')));
                } else {
                  Scaffold
                      .of(context)
                      .showSnackBar(SnackBar(content: Text('登陆失败')));
                  setState(() {
                    _passCon.text = "";
                    _userNameCon.text = "";
                  });
                }
              },
              color: Colors.red,
              child: Text('登陆'),
            );
          }),
        ],
      ),
    );
  }
}
